package com.yogee.topseller.activity.persenter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;

import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.topseller.activity.view.UploadVoucherVimpl;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.FileStorage;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/7/3.
 */

public class UploadVoucherPersenter implements UploadVoucherPimpl {
    private UploadVoucherVimpl v;

    public UploadVoucherPersenter(UploadVoucherVimpl v) {
        this.v = v;
    }

    @Override
    public void toCamera(Context context, String type) {
        Intent intent = new Intent();
        Uri uri;
        switch (type) {
            case "1":
                File file = new FileStorage().createIconFile();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(context, "com.yogee.topseller.FileProvider", file);//通过FileProvider创建一个content类型的Uri
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
                } else {
                    uri = Uri.fromFile(file);
                }
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//将拍取的照片保存到指定URI
                v.toCameraForResult(intent, type, file);
                break;
            case "2":
                File backFile = new FileStorage().createIconFile();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(context, "com.yogee.topseller.FileProvider", backFile);//通过FileProvider创建一个content类型的Uri
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
                } else {
                    uri = Uri.fromFile(backFile);
                }
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//将拍取的照片保存到指定URI
                v.toCameraForResult(intent, type, backFile);
                break;
            case "3":
                File holdFile = new FileStorage().createIconFile();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(context, "com.yogee.topseller.FileProvider", holdFile);//通过FileProvider创建一个content类型的Uri
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
                } else {
                    uri = Uri.fromFile(holdFile);
                }
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//将拍取的照片保存到指定URI
                v.toCameraForResult(intent, type, holdFile);
                break;
            case "4":
                File cardDoldFile = new FileStorage().createIconFile();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(context, "com.yogee.topseller.FileProvider", cardDoldFile);//通过FileProvider创建一个content类型的Uri
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
                } else {
                    uri = Uri.fromFile(cardDoldFile);
                }
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//将拍取的照片保存到指定URI
                v.toCameraForResult(intent, type, cardDoldFile);
                break;
            case "5":
                File cardBackFile = new FileStorage().createIconFile();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    uri = FileProvider.getUriForFile(context, "com.yogee.topseller.FileProvider", cardBackFile);//通过FileProvider创建一个content类型的Uri
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加这一句表示对目标应用临时授权该Uri所代表的文件
                } else {
                    uri = Uri.fromFile(cardBackFile);
                }
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//设置Action为拍照
                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//将拍取的照片保存到指定URI
                v.toCameraForResult(intent, type, cardBackFile);
                break;
        }
    }

    @Override
    public void toGallery(String type) {
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        v.toGalleryForResult(i, type);
    }

    @Override
    public void credentials(String orderId, String userId, File file, File file1, File file2, File file3, File file4) {
//        Bitmap bitmap = BitmapFactory.decodeFile(file.getPath(), getBitmapOption(shansou));
//        saveBitmapFile(file.getPath(), bitmap);
//        Bitmap bitmap1 = BitmapFactory.decodeFile(file1.getPath(), getBitmapOption(2));
//        saveBitmapFile(file1.getPath(), bitmap1);
//        Bitmap bitmap2 = BitmapFactory.decodeFile(file2.getPath(), getBitmapOption(2));
//        saveBitmapFile(file2.getPath(), bitmap2);
//        Bitmap bitmap3 = BitmapFactory.decodeFile(file3.getPath(), getBitmapOption(2));
//        saveBitmapFile(file3.getPath(), bitmap3);
//        Bitmap bitmap4 = BitmapFactory.decodeFile(file4.getPath(), getBitmapOption(2));
//        saveBitmapFile(file4.getPath(), bitmap4);
//        
        HttpManager.getInstance().credentials(orderId, userId, file, file1, file2, file3, file4).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean>() {
            @Override
            public void onNext(ResultBean o) {
                if (o.getResult().equals("1")) {
                    v.credentialsSuccess();
                }
            }
        }, v));
    }

    private BitmapFactory.Options getBitmapOption(int inSampleSize) {
        System.gc();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPurgeable = true;
        options.inSampleSize = inSampleSize;
        return options;
    }

    public void saveBitmapFile(String path, Bitmap bitmap) {
        File file = new File(path);//将要保存图片的路径 
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}